package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketCart;
import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.common.CouponUserConstant;
import com.cskaoyan.bean.vo.wx.coupon.WxUserCouponVo;
import com.cskaoyan.service.admin.CouponService;
import com.cskaoyan.service.wx.CartService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author by yxy_8175@163.com
 * @Description TODO
 * @Date 2022-11-22 9:57
 */

@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {


    @Autowired
    private CouponService couponService;
    @Autowired
    private CartService cartService;

    /*
     * @Description 用户个人优惠券展示
     * @param
     * @return
     * @date 2022-11-22 10:14
     * @auther yxy_8175@163.com
     */
    @GetMapping("mylist")
    public BaseRespVo myCouponList(BasePageInfo pageInfo, Integer status){
        // 1.认证用户，获取用户id
        // 2.调用service来查询详细信息
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal =(MarketUser) subject.getPrincipal();
        Integer userId = principal.getId();
        // Integer userId = subject.getId();
        CommonData<WxUserCouponVo> commonData = couponService.queryCouponByUserId(pageInfo, userId, status);
        return BaseRespVo.ok(commonData);
    }

    /*
     * @Description 平台优惠券展示
     * @param
     * @return
     * @date 2022-11-22 10:18
     * @auther yxy_8175@163.com
     */
    @GetMapping("list")
    public BaseRespVo AllCouponList(BasePageInfo pageInfo, String name, Integer type, Integer status) {

        CommonData<MarketCoupon> commonData = couponService.selectByPage(pageInfo, name, type, status);

        return BaseRespVo.ok(commonData);
    }

    /*
     * @Description 获取优惠券
     * @param
     * @return
     * @date 2022-11-22 10:21
     * @auther yxy_8175@163.com
     */
    @PostMapping("receive")
    public BaseRespVo receiveCoupon(@RequestBody Map map) {
        // 1.获取用户id
        Integer couponId = (Integer) map.get("couponId");
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal =(MarketUser) subject.getPrincipal();
        if (principal == null){
            return BaseRespVo.error(501,"请登录");
        }
        Integer userId = principal.getId();
        // 2.判断是否具有领取权限
        boolean flag = couponService.queryHaveQuota(userId, couponId);
        if (flag) {
            // 给用户添加优惠券
            boolean hasOk = couponService.receiveCouponForUser(userId, couponId);
            if (hasOk) {
                return BaseRespVo.ok();
            }
        }
        return BaseRespVo.error(BaseRespVo.STATUS_ALREADY_RECEIVE, "优惠券已经领取过");
    }

    /*
     * @Description 购物车下单时点击优惠券，会获得所有已获得 且 可使用的优惠券
     * @param
     * @return
     * @date 2022-11-22 15:01
     * @auther yxy_8175@163.com
     */
    @GetMapping("selectlist")
    public BaseRespVo selectListForBuyCar(Integer cartId, Integer grouponRulesId) {

        //判断是否登录
        //
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal =(MarketUser) subject.getPrincipal();
        if (principal == null){
            return BaseRespVo.error(501,"请登录");
        }
        Integer userId = principal.getId();

        // 计算商品价格：计算购物车中所有已选择的商品 计算价格总和
        List<MarketCart> checkedGoodsList = null;
        if (cartId == null || cartId.equals(0)) {
            checkedGoodsList = cartService.queryUidAndChecked(userId);
        } else {
            // 根据商品id和用户id
            MarketCart cart = cartService.findById(userId, cartId);
            if (cart == null) {
                return BaseRespVo.invalidParameter("无效的参数");
            }
            checkedGoodsList = new ArrayList<>(1);
            checkedGoodsList.add(cart);
        }
        // 获取商品价格
        BigDecimal checkedPrice = new BigDecimal(0.00);
        for (MarketCart cart : checkedGoodsList) {
            // 计算价格
            checkedPrice =checkedPrice.add(BigDecimal.valueOf(cart.getPrice()*(cart.getNumber())));
        }


        // 获取用户所有可用的优惠券
        CommonData<WxUserCouponVo> couponVoList = couponService.queryAll(userId,checkedPrice);

        if (couponVoList == null){
            // 用户没有优惠券
            // 直接返回空
            return BaseRespVo.ok(couponVoList);
        }

        return BaseRespVo.ok(couponVoList);
    }

    /*
     * @Description 通过兑换码来获取优惠券
     * @param
     * @return
     * @date 2022-11-22 20:25
     * @auther yxy_8175@163.com
    */
    @PostMapping("exchange")
    public BaseRespVo exchangeCoupon(@RequestBody Map map){
        // 获取用户ID
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal =(MarketUser) subject.getPrincipal();
        if (principal == null){
            return BaseRespVo.error(501,"请登录");
        }
        Integer userId = principal.getId();

        String code = (String) map.get("code");
        Short flag = couponService.exchangeCouponByCDK(userId,code);
        if (flag == CouponUserConstant.CDK_HAS_FAILED){
            return BaseRespVo.error("优惠券领取失败");
        }else if (flag == CouponUserConstant.CDK_OUT_OF_USE_TIME){
            return BaseRespVo.error("优惠券已经失效");
        }else if (flag == CouponUserConstant.CDK_ALREADY_HAVE){
            return BaseRespVo.error(BaseRespVo.STATUS_ALREADY_RECEIVE,"优惠券已经领取过");
        }
        return BaseRespVo.ok();
    }

}
